home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / whisperBack / encryption.py < prev    next >
Encoding:
Python Source  |  2012-12-13  |  2.4 KB  |  74 lines

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. ########################################################################
  5. # WhisperBack - Send feedback in an encrypted mail
  6. # Copyright (C) 2009-2012 Tails developers <amnesia.org>
  7. #
  8. # This file is part of WhisperBack
  9. #
  10. # WhisperBack is  free software; you can redistribute  it and/or modify
  11. # it under the  terms of the GNU General Public  License as published by
  12. # the Free Software Foundation; either  version 3 of the License, or (at
  13. # your option) any later version.
  14. # This program  is distributed in the  hope that it will  be useful, but
  15. # WITHOUT   ANY  WARRANTY;   without  even   the  implied   warranty  of
  16. # MERCHANTABILITY  or FITNESS  FOR A  PARTICULAR PURPOSE.   See  the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. ########################################################################
  22.  
  23. """Some tools for encryption
  24.  
  25. """
  26. import os.path
  27.  
  28. import GnuPGInterface
  29.  
  30. import whisperBack.exceptions
  31.  
  32. class Encryption (GnuPGInterface.GnuPG):
  33.     """Some tools for encryption"""
  34.  
  35.     def __init__ (self, keyring=None):
  36.         """Initialize the encryption mechanism"""
  37.  
  38.         GnuPGInterface.GnuPG.__init__(self)
  39.  
  40.         self.options.armor = True
  41.         self.options.meta_interactive = False
  42.         self.options.always_trust = True
  43.  
  44.         if keyring and os.path.exists(keyring):
  45.             self.options.extra_args = ["--keyring", keyring, "--no-default-keyring"]
  46.  
  47.     def encrypt (self, data, to_fingerprints):
  48.         """Encrypts data for a list of recepients
  49.         
  50.         @param to_fingerprints A list of recepient's key fingerprints
  51.         @param data Data to be encrypted
  52.         @return The encrypted data
  53.         """
  54.         try:
  55.             self.options.recipients = to_fingerprints
  56.             proc = self.run(['--encrypt'], create_fhs=['stdin', 'stdout', 'stderr'])
  57.  
  58.             proc.handles['stdin'].write(data)
  59.             proc.handles['stdin'].close()
  60.  
  61.             output = proc.handles['stdout'].read()
  62.             proc.handles['stdout'].close()
  63.  
  64.             error = proc.handles['stderr'].read()
  65.             proc.handles['stderr'].close()
  66.  
  67.             proc.wait()
  68.             return output
  69.  
  70.         except IOError, e:
  71.             # XXX: raise a specific exception if the key wasn't found
  72.             raise whisperBack.exceptions.EncryptionException(error)
  73.